import time
import pymysql
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.product_management_locator import ProductManagementLocator
from Case.python_ui.PageLocate.user_oss_locator import UserOssLocator


class  UserOssPages(Basepage):
    def query_oss_instance_from_db(self):
        conn = pymysql.connect(host="10.10.220.124", port=3306, user="root", passwd="cre@mysql8.0",
                           db="cre-sigma-test-s3", charset="utf8mb4")
        mycursor = conn.cursor()
        query_oss_instance_sql = "select resource_id from instance_offer where cust_id=1309 and offer_id=10066 and instance_state='OSN' limit 1"
        print(query_oss_instance_sql)
        mycursor.execute(query_oss_instance_sql)
        query_result = mycursor.fetchone()
        oss_ins = ''.join(query_result)
        print(oss_ins)
        conn.commit()
        conn.close()
        return oss_ins
    def query_from_db(self, sql1):
        conn = pymysql.connect(host="10.10.220.124", port=3306, user="root", passwd="cre@mysql8.0",
                           db="cre-sigma-test-s3", charset="utf8mb4")
        mycursor = conn.cursor()
        print(sql1)
        mycursor.execute(sql1)
        query_result = mycursor.fetchone()
        result = ''.join(query_result)
        print(result)
        conn.commit()
        conn.close()
        return result


    def get_resouce_name_to_click(self,resource_name):
        doc='获取到资源名称然后点击'
        resource_list = self.get_elements(ProductManagementLocator.resources, doc)
        for resource in  resource_list:
            if resource.text==resource_name:
                resource.click()
        time.sleep(2)


    def get_prod_name_to_click(self,prod_name):
        doc='获取产品名称然后点击'
        prod_list=self.get_elements(ProductManagementLocator.prod_list,doc)
        time.sleep(2)
        for prod in prod_list:
            if prod.text == prod_name:
                prod.click()
        time.sleep(2)

    def enter_my_resource_page(self):
        doc='进入user端的myresource页面'
        for i in range(3):
            try:
                self.wait_elevisble(ProductManagementLocator.my_resource_tab, doc)
                self.click_element(ProductManagementLocator.my_resource_tab, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(ProductManagementLocator.page_total,doc)


    def enter_resource_package_page(self):
        doc='进入user端的myresource页面'
        for i in range(3):
            try:
                self.wait_elevisble(ProductManagementLocator.resource_package, doc)
                self.click_element(ProductManagementLocator.resource_package, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(ProductManagementLocator.page_total,doc)
    def get_product_detail(self):
        doc='获取产品的detail'
        eles=self.get_elements(ProductManagementLocator.detail_element,doc)
        detail_list=[ele.text for ele in eles]
        print(detail_list)
        return detail_list


    def get_order_details(self):
        doc='获取订单中的details'
        eles=self.get_elements(ProductManagementLocator.order_details,doc)
        order_detail_list=[ele.text for ele in eles]
        del order_detail_list[-1]
        del order_detail_list[-1]
        print(order_detail_list)
        new_list = []
        for i in order_detail_list:
            new_list.append(i.split(': ')[1])
        print(new_list)
        return new_list

    def order_oss_standard_storage_plan(self):
        doc='订购Outbound Traffic Plan的OSS'
        self.enter_my_resource_page()
        self.get_resouce_name_to_click('Storage')
        self.wait_elevisble(ProductManagementLocator.cerate_new_tab, doc)
        self.move_mouse_to_element(ProductManagementLocator.cerate_new_tab)
        time.sleep(2)
        self.get_prod_name_to_click('Object Storage Service')
        time.sleep(2)
        self.switch_to_new_window()
        self.wait_eleinvisble(ProductManagementLocator.loading_wait, doc)
        time.sleep(5)
        self.click_element(UserOssLocator.standard_storage_plan, doc)
        time.sleep(1)
        self.click_element(ProductManagementLocator.term_of_service, doc)
        time.sleep(1)
        self.click_element(ProductManagementLocator.buy_now_button, doc)
        self.wait_elevisble(ProductManagementLocator.view_detail, doc)
        self.move_mouse_to_element(ProductManagementLocator.view_detail)
        time.sleep(2)
        detail = self.get_product_detail()
        time.sleep(1)
        self.click_element(ProductManagementLocator.pay_button, doc)
        self.wait_elevisble(ProductManagementLocator.pay_successful, doc)
        self.click_element(ProductManagementLocator.check_order_button, doc)
        self.wait_elevisble(ProductManagementLocator.order_view_detail_button, doc)
        eles = self.get_elements(ProductManagementLocator.order_view_detail_button, doc)
        eles[0].click()
        self.wait_elevisble(ProductManagementLocator.order_details, doc)
        order_detail = self.get_order_details()
        self.driver.close()
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[0])
        return detail, order_detail
    def renew_oss_standard_storage(self):
        doc = '续订OSS Standard Storage Plan'
        self.enter_resource_package_page()
        self.wait_elevisble(UserOssLocator.package_id_search, doc)
        oss_instance_id = self.query_oss_instance_from_db()
        self.send_keys(UserOssLocator.package_id_search, oss_instance_id, doc)
        self.click_element(UserOssLocator.search_btn, doc)
        time.sleep(3)
        self.wait_elevisble(UserOssLocator.renew_btn, doc)
        self.click_element(UserOssLocator.renew_btn, doc)
        try:
            self.switch_to_new_window()
            time.sleep(2)
            self.wait_elevisble(ProductManagementLocator.term_of_service, doc)
            self.click_element(ProductManagementLocator.term_of_service, doc)
            time.sleep(2)
            self.click_element(ProductManagementLocator.buy_now_button, doc)
            self.wait_elevisble(ProductManagementLocator.pay_button, doc)
            time.sleep(2)
            self.click_element(ProductManagementLocator.pay_button, doc)
            time.sleep(5)
            self.wait_elevisble(ProductManagementLocator.pay_successful, doc)
        except:
            self.driver.close()
            all_handles = self.driver.window_handles
            self.driver.switch_to.window(all_handles[0])
    def oss_configuration_change(self):
        doc = '变配OSS'
        self.enter_resource_package_page()
        self.wait_elevisble(UserOssLocator.package_id_search, doc)
        oss_instance_id = self.query_oss_instance_from_db()
        self.send_keys(UserOssLocator.package_id_search, oss_instance_id, doc)
        self.click_element(UserOssLocator.search_btn, doc)
        time.sleep(3)
        self.wait_elevisble(UserOssLocator.modify_btn,doc)
        self.click_element(UserOssLocator.modify_btn,doc)
        time.sleep(2)
        try:
            self.switch_to_new_window()
            self.wait_elevisble(ProductManagementLocator.term_of_service, doc)
            time.sleep(2)
            self.click_element(ProductManagementLocator.term_of_service, doc)
            time.sleep(2)
            self.click_element(ProductManagementLocator.buy_now_button, doc)
            self.wait_elevisble(ProductManagementLocator.pay_button, doc)
            time.sleep(2)
            self.click_element(ProductManagementLocator.pay_button, doc)
            time.sleep(5)
            self.wait_elevisble(ProductManagementLocator.pay_successful, doc)
        except:
            self.driver.close()
            all_handles = self.driver.window_handles
            self.driver.switch_to.window(all_handles[0])

    def  order_express_connect(self):
        doc='订购express_connect'
        self.enter_my_resource_page()
        self.get_resouce_name_to_click('Network')
        self.wait_elevisble(ProductManagementLocator.cerate_new_tab, doc)
        self.move_mouse_to_element(ProductManagementLocator.cerate_new_tab)
        time.sleep(2)
        self.get_prod_name_to_click('Express Connect')
        time.sleep(2)
        self.switch_to_new_window()
        self.wait_eleinvisble(ProductManagementLocator.loading_wait, doc)
        time.sleep(5)
        self.move_mouse_to_element(ProductManagementLocator.term_of_service)
        self.click_element(ProductManagementLocator.term_of_service, doc)
        time.sleep(1)
        self.click_element(ProductManagementLocator.buy_now_button, doc)
        self.wait_elevisble(ProductManagementLocator.view_detail, doc)
        self.move_mouse_to_element(ProductManagementLocator.view_detail)
        time.sleep(2)
        detail = self.get_product_detail()
        time.sleep(1)
        self.click_element(ProductManagementLocator.pay_button, doc)
        self.wait_elevisble(ProductManagementLocator.pay_successful, doc)
    def renew_express_connect(self):
        doc = '续订express_connect'
        self.enter_my_resource_page()
        sql1 = "select resource_id  from instance_offer where cust_id=1309 and offer_id=10200 and instance_state='OSN' limit 1"
        express_cmm_id = self.query_from_db(sql1)
        self.wait_elevisble(UserOssLocator.instance_id_input, doc)
        self.send_keys(UserOssLocator.instance_id_input, express_cmm_id, doc)
        time.sleep(1)
        self.click_element(UserOssLocator.search_btn, doc)
        time.sleep(3)
        self.wait_elevisble(UserOssLocator.renew_btn, doc)
        self.click_element(UserOssLocator.renew_btn, doc)
        try:
            self.switch_to_new_window()
            time.sleep(2)
            self.wait_elevisble(ProductManagementLocator.term_of_service, doc)
            self.click_element(ProductManagementLocator.term_of_service, doc)
            time.sleep(2)
            self.click_element(ProductManagementLocator.buy_now_button, doc)
            self.wait_elevisble(ProductManagementLocator.pay_button, doc)
            time.sleep(2)
            self.click_element(ProductManagementLocator.pay_button, doc)
            time.sleep(5)
            self.wait_elevisble(ProductManagementLocator.pay_successful, doc)
        except:
            self.driver.close()
            all_handles = self.driver.window_handles
            self.driver.switch_to.window(all_handles[0])

    def order_DDos(self):
        doc='订购DDos'
        self.enter_my_resource_page()
        self.get_resouce_name_to_click('Security')
        self.wait_elevisble(ProductManagementLocator.cerate_new_tab, doc)
        self.move_mouse_to_element(ProductManagementLocator.cerate_new_tab)
        time.sleep(2)
        self.get_prod_name_to_click('Anti-DDoS')
        time.sleep(2)
        self.switch_to_new_window()
        self.wait_eleinvisble(ProductManagementLocator.loading_wait, doc)
        time.sleep(5)
        self.click_element(ProductManagementLocator.buy_now_button, doc)
        self.click_element(ProductManagementLocator.term_of_service, doc)
        time.sleep(1)
        self.wait_elevisble(ProductManagementLocator.view_detail, doc)
        self.move_mouse_to_element(ProductManagementLocator.view_detail)
        time.sleep(2)
        detail = self.get_product_detail()
        time.sleep(1)
        self.click_element(ProductManagementLocator.pay_button, doc)
        time.sleep(5)
        self.wait_elevisble(ProductManagementLocator.pay_successful, doc)
        assert self.get_element(ProductManagementLocator.pay_successful, doc).text == 'Thank you for your payment. You can view the order details in order management or open the console for related operations.'

    def enter_oss_console(self):
        doc='OSS控制台'
        self.click_element(ProductManagementLocator.menu_list,doc)
        self.wait_elevisble(UserOssLocator.product_list_oss,doc)
        time.sleep(2)
        self.click_element(UserOssLocator.product_list_oss,doc)




